package com.wwfish.cmsui.modules.channel.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.ui.HasModel;
import com.nexustar.gwt.widgets.client.ui.ModelManager;
import com.nexustar.gwt.widgets.client.ui.panel.ContentPanel;
import com.nexustar.gwt.widgets.client.ui.panel.InfoPanel;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolItem;
import com.wwfish.cms.service.ChannelManager;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;
import com.wwfish.cmsui.modules.common.client.widget.xmlform.XMLFormContainer;
import com.wwfish.gwt.reflection.client.ModelReflection;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-4
 * Time: 15:03:37
 * To change this template use File | Settings | File Templates.
 */
public class ChannelWidget extends Composite implements HasModel {
    private XMLFormContainer form;

    private ModelManager mm;
    private ContentPanel wrapper;
    private ToolBar bar;
    private IAsyncModelCallback saveFinishModelCallback;

    public ChannelWidget(IAsyncModelCallback saveFinishModelCallback) {
        this.saveFinishModelCallback = saveFinishModelCallback;
        initBar() ;
        form = new XMLFormContainer("channel-form.xml", 2);
        form.setWidth("100%");
        wrapper = new ContentPanel("栏目详情");
        VerticalPanel vp = new VerticalPanel();
        vp.setWidth("100%");
        vp.add(bar);
        vp.add(form);
        wrapper.addContentWidget(vp);
        initWidget(wrapper);

    }

    private void initBar() {
        bar = new ToolBar();
        bar.setWidth("100%");
        ToolItem save = new ToolItem("确认保存");
        bar.addToolItem(save);

        save.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                if (!form.isValid()) return;
                ServiceFactory.invoke(ChannelManager.class.getName(), "saveAndUpdate", new Object[]{form.getModelManger().getModel()},
                        new FishAsyncCallback() {
                            public void onSuccess(Object o) {
                                saveFinishModelCallback.setModelElments(o);
                                InfoPanel.show("保存成功！");
                            }
                        });
            }
        });


    }

    public void ddOutModel(Object[] parameters, Object model) {
        form.getModelManger().renderModel(ModelReflection.clone(model));
    }

    public void setTitle(String text) {
        wrapper.setTitle(text);
    }

    public void ddIntModel(Object model) {
    }

    public ModelManager getModelManger() {
        if (mm == null)
            mm = new ModelManager(this);
        return mm;
    }
}
